import { Article } from '../database/database'
import Sequelize from 'sequelize'
const Op = Sequelize.Op

export default {
    randFee : async() =>{
        return await Article.findOne({ where :{ status : 1, fee : { [Op.not] : 0 } }, order: Sequelize.fn('RAND') })
    },
    rand : async() => { 
        return await Article.findOne({ where:{ status : 1 }, order: Sequelize.fn('RAND') })
    },
    bannersByFee : async(page = 1) =>{
        let data = new Date().getTime() - (30 * 24 * 60 * 60 * 1000)
        return await Article.findAndCountAll({ where :{
                fee : { [Op.not] : 0 },
                status : 1,
                createdAt : { [Op.gt] : data }
            },
            limit: 10,
            offset : 10 * (page - 1),
            order : [ ['fee', 'DESC'], ['createdAt','DESC'] ]
        })
    },
    bannersByNotFee : async(page = 1) =>{
        let data = new Date().getTime() - (30 * 24 * 60 * 60 * 1000)
        return await Article.findAndCountAll({ where :{ 
                fee : 0,
                status : 1,
                createdAt : { [Op.gt] : data }
            },
            limit: 10,
            offset : 10 * (page - 1)
        })
    },
    save : async(data) =>{
        return await Article.create(data)
    },
    get : async(id) =>{
        return await Article.findById(id)
    },
    findByUser : async(user) =>{
        let data = new Date().getTime() - (30 * 24 * 60 * 60 * 1000)
        return await Article.findOne({ where :{
            user : user,
            status : 1,
            createdAt : { [Op.gt] : data }
        },
        order : [ ['fee', 'DESC'], ['createdAt','DESC'] ]
    })
    },
    count : async() =>{
        return await Article.count()
    }
}